package com.itheima.controller;

import com.itheima.anno.Flag;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理
 */
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 员工查询
     *
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result queryEmp(String name,
                           Integer gender,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Emp> pageResult = empService.queryEmp(name, gender, begin, end, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 员工添加
     *
     * @param emp
     * @return
     */
    @PostMapping
    @Flag
    public Result add(@RequestBody Emp emp) throws Exception {

        empService.add(emp);
        return Result.success();
    }

    /**
     * 员工删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @Flag
    public Result delete(/*@RequestParam("ids")*/ Integer[] ids) {
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 员工查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     * 员工修改
     *
     * @param emp
     * @return
     */
    @PutMapping
    @Flag
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

    /**
     * 员工列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Emp> list = empService.list();
        return Result.success(list);
    }
}
